package com.cgbl.service.adapter

import android.graphics.Bitmap
import android.view.View
import android.widget.ImageView
import com.cgbl.service.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.othershe.combinebitmap.CombineBitmap
import com.othershe.combinebitmap.layout.WechatLayoutManager
import com.othershe.combinebitmap.listener.OnProgressListener
import com.robot.base.model.MemberAdapterBean
import com.robot.base.model.TeamListBean


class TeamManagerAdapter(
    private val checkEnable: Boolean = false
) : BaseQuickAdapter<TeamListBean, BaseViewHolder>(R.layout.app_item_team_manager) {

    var selectPos = -1
    override fun convert(holder: BaseViewHolder, item: TeamListBean) {

        val checkView = holder.getView<ImageView>(R.id.iv_check)
        checkView.visibility = if (checkEnable) View.VISIBLE else View.GONE
        checkView.setOnClickListener {
            selectPos = holder.adapterPosition
            notifyDataSetChanged()
        }
        checkView.isSelected = selectPos == holder.adapterPosition


        holder.setText(R.id.tv_name, item.teamName)
        CombineBitmap.init(context)
            .setLayoutManager(WechatLayoutManager()) // 必选， 设置图片的组合形式，支持WechatLayoutManager、DingLayoutManager
            .setSize(40) // 必选，组合后Bitmap的尺寸，单位dp
            .setPlaceholder(R.drawable.ps_ic_placeholder) // 单个图片加载失败的默认显示图片
            .setUrls(*(item.heads?.toTypedArray() ?: arrayOf())) // 要加载的图片url数组
            .setImageView(holder.getView(R.id.iv_avatar)) // 直接设置要显示图片的ImageView
            // 设置“子图片”的点击事件，需使用setImageView()，index和图片资源数组的索引对应
            .setOnSubItemClickListener { } // 加载进度的回调函数，如果不使用setImageView()方法，可在onComplete()完成最终图片的显示
            .setOnProgressListener(object : OnProgressListener {
                override fun onStart() {}
                override fun onComplete(bitmap: Bitmap?) {}
            }).build()
    }

    fun cancelAllCheck() {
        selectPos = -1
        notifyDataSetChanged()
    }

    fun getSelectedMember(): TeamListBean? {
        if (selectPos == -1) return null
        var selectMembers: TeamListBean? = null
        for (i in 0 until data.size) {
            if (selectPos == i) {
                selectMembers = data[i]
            }
        }
        return selectMembers
    }

}